import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper, DrawerHelper, SettingsService } from '@delon/theme';
import { NzNotificationService, NzMessageService, NzModalService } from 'ng-zorro-antd';
import { SFSchema } from '@delon/form';
import { STColumn, STData, STComponent } from '@delon/abc';
import { RoleModalComponent } from './edit/role-modal.component';
import { MenuAuthModalComponent } from './auth/menu-auth-modal.component';
import { applyCC } from '@shared';

@Component({
  selector: 'role-basic',
  templateUrl: './role-basic.component.html',
})
export class RoleBasicComponent implements OnInit {
  constructor(
    private http: _HttpClient,
    private notice: NzNotificationService,
    private msg: NzMessageService,
    private drawer: DrawerHelper,
    private settings: SettingsService,
  ) {}
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;
  auth_btn_perm = false;
  schema: SFSchema = {
    properties: {},
  };
  @ViewChild('st', { static: true }) private st: STComponent;
  params: any = {};
  url = '/cf/admin/role/page';
  columns: STColumn[] = applyCC([
    { title: '角色名称', index: 'roleName' },
    { title: '角色编码', index: 'roleCode' },
    { title: '描述', index: 'roleDesc' },
    {
      title: '数据权限',
      index: 'dsType',
      format: (item: STData, col: STColumn, index: number) => {
        const type = item.dsType;
        if (type === 0) {
          return '无限制';
        } else if (type === 1) {
          return '自定义';
        } else if (type === 2) {
          return '本部门及子部门';
        } else if (type === 3) {
          return '本部门';
        }
        return '未定义';
      },
    },
    { title: '创建日期', index: 'createTime', type: 'date' },
    { title: '更新日期', index: 'updateTime', type: 'date', ccChecked: false },
    {
      ccDisabled: true,
      title: '操作',
      buttons: [
        {
          text: '修改',
          type: 'drawer',
          drawer: {
            title: '修改角色',
            component: RoleModalComponent,
          },
          click: 'load',
          iif: (item: STColumn) => this.auth_btn_edit,
        },
        {
          text: '删除',
          type: 'del',
          pop: {
            title: '确定删除角色？',
            okType: 'danger',
          },
          click: (record, _modal, comp) => {
            this.http.delete(`/cf/admin/role/${record.roleId}`).subscribe(res => {
              if (res.status === 200) {
                this.notice.success('成功', `成功删除【${record.roleName}】`);
                comp!.load();
              }
            });
          },
          iif: (item: STColumn) => this.auth_btn_del,
        },
        {
          text: '权限',
          type: 'modal',
          modal: {
            component: MenuAuthModalComponent,
            paramsName: 'record',
          },
          click: (_record, modal) => this.msg.success(`重新加载页面，回传值：${JSON.stringify(modal)}`),
          iif: (item: STColumn) => this.auth_btn_perm,
        },
      ],
    },
  ]);

  ngOnInit(): void {
    this.auth_btn_add = this.settings.user.permissions.sys_role_add;
    this.auth_btn_edit = this.settings.user.permissions.sys_role_edit;
    this.auth_btn_del = this.settings.user.permissions.sys_role_del;
    this.auth_btn_perm = this.settings.user.permissions.sys_role_perm;
  }

  add() {
    this.drawer.create('添加角色', RoleModalComponent).subscribe(() => this.st.load());
  }
}
